package org.apache.ibatis.demo.test;

import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.demo.dao.BlogMapper;
import org.apache.ibatis.demo.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Date 2021/2/23 23:07
 * @Created mobian
 * @Description 测试动态代理方法
 */
public class Demo3 {
  public static void main(String[] args) throws IOException {
//    SqlSession sqlSession = MyBatisUtil.getSqlSession();
//    BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
//    List<Integer> integers = mapper.queryBlogList(new String("1"));
//    for (int i : integers) {
//      System.out.println("结果是：" + i);
//
//    }
    SqlSession sqlSession = MyBatisUtil.getSqlSession();
    BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
    List<Map<String, Object>> lists= mapper.queryBlogListById("2");


  }


  @Test
  public void testProxy() {
    //在执行对应的接口方法的时候，去执行下面的逻辑
    BlogMapper blogMapper = (BlogMapper) Proxy.newProxyInstance(Demo3.class.getClassLoader(), new Class<?>[]{BlogMapper.class}, new InvocationHandler() {
      @Override
      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        //获取方法上面含有注解的，并且注解是select的
        Select annotation = method.getAnnotation(Select.class);
        System.out.println(annotation);

        //获取方法的参数，并输出
        System.out.println(Arrays.toString(args));

        //获取方法的注解，为update的
        Update annotation2 = method.getAnnotation(Update.class);
        System.out.println(annotation2);

        if(annotation != null) {
          //打印输出方法注解中的value值
          String[] value = annotation.value();
          System.out.println(Arrays.toString(value));
        }

        System.out.println("这是代理里面的方法");
        return null;
      }
    });
    String str = "11";
    blogMapper.queryBlogList(str);
    blogMapper.updateBlog(str);

  }
}
